/**@author Jason 吕建辉
 * @data 2018-04-07
 * @描述：模板组件采用 layui-2.2.6 操作手册可参考管饭文档
 */
//提取弹窗组件为公共
var layer;
layui.use(['table', 'layer', 'form'], function () {
    var table = layui.table, form = layui.form;
    layer = layui.layer;
    var tableIns = table.render({
        id: 'table',
        elem: '#table',
        url: '/rbac/task/list',
        where: $('#searchFrom').containerJson(),
        method: 'get', //如果无需自定义HTTP类型，可不加该参数
        request: pageRequest,
        response: pageResponse,
        page: pagenation,
        cellMinWidth: 60,//全局定义常规单元格的最小宽度
        cols: [[
            {
                field: 'name',
                title: '任务名称',
                align: 'center'
            }, {
                field: 'cron',
                title: '表达式',
                align: 'center'
            }, {
                field: 'service',
                title: '任务实现类',
                align: 'center',
            }, {
                field: 'method',
                title: '任务实现方法',
                align: 'center'
            }, {
                field: 'params',
                title: '任务参数',
                align: 'center'
            }, {
                field: 'latestLog',
                title: '上次执行结果',
                align: 'center',
                templet: function (data) {
                    return data.latestLog.result;
                }
            }, {
                field: 'createdTime',
                title: '创建时间',
                align: 'center',
                templet: function (data) {
                    return dateFormatter(data.createdTime);
                }
            }, {
                fixed: 'right',
                title: '操作',
                align: 'center',
                toolbar: '#toolbar'
            }]]
    });
    //绑定操作
    var $2 = layui.$, active = {
        reset: function () {//重置
            clearSearch();
        },
        add: function () {//新增
            $('#addOrUpdate').form('clear');
            removeReadOnly();
            openForm('addOrUpdate', tableIns);
        }
    };
    $2('.layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //监听 查询表单提交
    form.on('submit(search)', function (data) {
        reloadTable(tableIns, data.field);
        return false;
    });
    //表格最后一列操作按钮监听
    table.on('tool(table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            deleteById(data.id, tableIns);
        } else if (obj.event === 'edit') {
            $('#addOrUpdate').form('load', data);
            addReadOnly();
            openForm('addOrUpdate', tableIns);
        } else if (obj.event === 'run') {
            var taskIds = [];
            taskIds.push(data.id);
            runTask(taskIds);
        }
    });
});

//清空查询条件
function clearSearch() {
    $('.layui-input').val("");
}

//打开表单
function openForm(formId, tableIns) {
    layer.open({
        type: 1,
        title: '任务操作',
        shadeClose: true,
        skin: 'layui-layer-rim',
        anim: 2,
        shade: 0.3,
        maxmin: true,
        area: ['50%', '70%'],
        content: $('#' + formId),//用id时 ，type 必须 =1
        btn: ['提交'],
        yes: function (i) {
            commitForm(tableIns);
        }
    });
}

//表单
function commitForm(tableIns) {
    var data = $('#addOrUpdate').serializeJson();
    var url = '/rbac/task/add';
    if (data.id) {
        url = '/rbac/task/update'
    }
    submitForm(url, data, tableIns);
}

function submitForm(url, data, tableIns) {
    $.ajax({
        url: url,
        data: data,
        method: 'post',
        success: function (res) {
            if (res.flag) {
                layer.closeAll();
                layer.msg('处理成功', {time: 2000}, function () {
                    reloadTable(tableIns);
                });
            } else {
                layer.msg(res.msg, {time: 2000});
            }
        }, error: function () {
            layer.msg('请求失败', {time: 2000});
        }
    });
}

function runTask(taskIds) {
    var index;
    $.ajax({
        url: '/rbac/task/run',
        method: 'post',
        data: {taskIds: taskIds},
        beforeSend: function () {
            index = layer.load(0, {shade: false});
        },
        success: function (res) {
            if (res.flag) {
                layer.msg(res.msg, {time: 2000});
            } else {
                layer.msg(res.msg, {time: 2000});
            }
        }, error: function () {
            layer.msg('请求失败', {time: 2000});
        },
        complete: function () {
            layer.close(index);
        }
    });
}

function addReadOnly() {
    $('#name').attr('readOnly', "readOnly");
    $('#service').attr('readOnly', "readOnly");
    $('#method').attr('readOnly', "readOnly");
}

function removeReadOnly() {
    $('#name').attr('readOnly', false);
    $('#service').attr('readOnly', false);
    $('#method').attr('readOnly', false);
}

function reloadTable(tableIns, data) {
    tableIns.reload({
        page: {curr: 1},
        where: data
    });
    return false;//防止页面刷新
}